﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using Galaktika.BI.Runtime.Services;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
using Galaktika.BI.Extensibility.Dom.Dac;
using Galaktika.BI.Silverlight.Services;
using Ranet.Olap.Core.Metadata;
using Ranet.AgOlap.Controls.General.ClientServer;
using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.DataAccess;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    public class GetMetadataCommand : Galaktika.BI.Silverlight.Commands.ServerCommand
    {
        public GetMetadataCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.GetMetadataCommandId)
        {
            this.InvokeSchema = invoker;
            this.ContextId = invoker[KnownInvokeArgs.ContextId];
            this.ConnectionId = invoker[KnownInvokeArgs.ConnectionId];
            this.Args = invoker[KnownInvokeArgs.MetadataQueryInfo];
        }

        public InvokeSchema InvokeSchema { get; private set; }
        public string ContextId { get; private set; }
        public string ConnectionId { get; private set; }
        public string Args { get; private set; }

        private IBindingContext Context
        {
            get
            {
                return DataBinder.Current.GetContext(this.ContextId);
            }
        }

        //OlapMetadataManager m_Provider = null;
        //private OlapMetadataManager DataProvider
        //{
        //    get
        //    {
        //        if (m_Provider == null)
        //        {
        //            BrickConnection conn = DataBinder.Current.GetNamedObject(this.ConnectionId) as BrickConnection;
        //            if (conn != null)
        //            {
        //                var exe = (IExecutionContext)Context.Services.GetService(typeof(IExecutionContext));
        //                if (exe != null)
        //                {
        //                    m_Provider = new OlapMetadataManager(exe.Parse(Context, conn.ConnectionString), this.InvokeSchema);
        //                }
        //            }
        //            else
        //            {
        //                throw new Exception(String.Format(Localization.Msg_ConnectionNotFound, this.ConnectionId));
        //            }
        //        }
        //        return m_Provider;
        //    }
        //}

        public override InvokeResultSchema Invoke()
        {
                MetadataQuery args = XmlUtility.XmlStr2Obj<MetadataQuery>(this.Args);
                BrickConnection conn = DataBinder.Current.GetNamedObject(this.ConnectionId) as BrickConnection;
                string connStr = string.Empty;
                if (conn != null)
                {
                    var exe = (IExecutionContext)Context.Services.GetService(typeof(IExecutionContext));
                    if (exe != null)
                    {
                        connStr = exe.Parse(Context, conn.ConnectionString);
                    }
                }
                else
                {
                    throw new Exception(String.Format(Localization.Msg_ConnectionNotFound, this.ConnectionId));
                }
                this.InvokeSchema[KnownInvokeArgs.MetadataQueryInfo] = XmlUtility.Obj2XmlStr(args);
                this.InvokeSchema.Args.Add(new InvokeArg(KnownInvokeArgs.ConnectionString, connStr));

                using (var srv = new SyncDataService())
                {
                    return srv.InvokeCommand(this.InvokeSchema);
                }
                //if (DataProvider != null)
                //{
                //    switch (args.QueryType)
                //    {
                //        case MetadataQueryType.GetMeasures:
                //            return GetMeasures(args);
                //        case MetadataQueryType.GetMeasure:
                //            return GetMeasure(args);
                //        case MetadataQueryType.GetKPIs:
                //            return GetKPIs(args);
                //        case MetadataQueryType.GetKPI:
                //            return GetKPI(args);
                //        case MetadataQueryType.GetLevels:
                //            return GetLevels(args);
                //        case MetadataQueryType.GetLevel:
                //            return GetLevel(args);
                //        case MetadataQueryType.GetDimensions:
                //            return GetDimensions(args);
                //        case MetadataQueryType.GetHierarchies:
                //            return GetHierarchies(args);
                //        case MetadataQueryType.GetDimension:
                //            return GetDimension(args);
                //        case MetadataQueryType.GetHierarchy:
                //            return GetHierarchy(args);
                //        case MetadataQueryType.GetLevelProperties:
                //            return GetLevelProperties(args);
                //        case MetadataQueryType.GetCubeMetadata:
                //            return GetCubeMetadata(args);
                //    }
                //}

                //var res = new InvokeResultSchema(base.Id);
                //res.ContentType = KnownContentTypes.Empty;
                //return res;
        }

        /*
        InvokeResultSchema GetCubeMetadata(MetadataQuery args)
        {
            return DataProvider.GetCubeMetadata(
                //args.CubeName, 
                //args.QueryType
                );
        }

        InvokeResultSchema GetLevelProperties(MetadataQuery args)
        {
            return DataProvider.GetLevelProperties();
        }

        InvokeResultSchema GetMeasures(MetadataQuery args)
        {
            return DataProvider.GetMeasures(
                //args.CubeName
                );
        }

        InvokeResultSchema GetMeasure(MetadataQuery args)
        {
            return DataProvider.GetMeasure(
                //args.CubeName, args.MeasureUniqueName
                );
        }

        InvokeResultSchema GetKPIs(MetadataQuery args)
        {
            return DataProvider.GetKPIs(
                //args.CubeName
                );
        }

        InvokeResultSchema GetKPI(MetadataQuery args)
        {
            return DataProvider.GetKPI(
                //args.CubeName, args.KPIName
                );
        }

        InvokeResultSchema GetLevel(MetadataQuery args)
        {
            return DataProvider.GetLevel(
                //args.CubeName, 
                //args.DimensionUniqueName, 
                //args.HierarchyUniqueName, 
                //args.LevelUniqueName
                );
        }

        InvokeResultSchema GetLevels(MetadataQuery args)
        {
            //InvokeResultSchema res = new InvokeResultSchema(base.Id);

            //Dictionary<String, LevelInfo> list = DataProvider.GetLevels(args.CubeName, args.DimensionUniqueName, args.HierarchyUniqueName);

            //res.ContentType = KnownContentTypes.MultidimData;
            //res.Content = XmlUtility.Obj2XmlStr(list.Values.ToList(), Common.Namespace);

            //return res;
            return DataProvider.GetLevels();
        }

        InvokeResultSchema GetDimensions(MetadataQuery args)
        {
            return DataProvider.GetDimensions(
                //args.CubeName
                );
        }

        InvokeResultSchema GetHierarchies(MetadataQuery args)
        {
            return DataProvider.GetHierarchies(
                //args.CubeName, 
                //args.DimensionUniqueName
                );
        }

        InvokeResultSchema GetDimension(MetadataQuery args)
        {
            //InvokeResultSchema res = new InvokeResultSchema(base.Id);

            //var invRes = DataProvider.GetDimensions(args.CubeName);
            //List<DimensionInfo> dims = XmlUtility.XmlStr2Obj<List<DimensionInfo>>(invRes.Content);
            //foreach (DimensionInfo info in dims)
            //{
            //    if (info.UniqueName == args.DimensionUniqueName)
            //    {
            //        res.Content = XmlUtility.Obj2XmlStr(info, Common.Namespace);
            //    }
            //}
            //res.ContentType = KnownContentTypes.MultidimData;

            //return res;

            return DataProvider.GetDimensions();
        }

        InvokeResultSchema GetHierarchy(MetadataQuery args)
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);

            var invRes = DataProvider.GetHierarchies(args.CubeName, args.DimensionUniqueName);
            List<HierarchyInfo> hiers = XmlUtility.XmlStr2Obj<List<HierarchyInfo>>(invRes.Content);
            foreach (HierarchyInfo info in hiers)
            {
                if (info.UniqueName == args.HierarchyUniqueName)
                {
                    res.Content = XmlUtility.Obj2XmlStr(info, Common.Namespace);
                }
            }
            res.ContentType = KnownContentTypes.MultidimData;

            return res;
        }
        */
    }
}
